package com.dianjing.dianqun.common.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudMpper<T>, T> {

	/**
	 * 持久层对象
	 */
	@Autowired
	protected D dao;

	/**
	 * 获取单条数据
	 * 
	 * @param id
	 * @return
	 */
	public T selectByPrimaryKey(Integer id) {
		return dao.selectByPrimaryKey(id);
	}

	/**
	 * 插入数据
	 * 
	 * @param entity
	 * @return
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ)
	public int insertSelective(T entity) {
		return dao.insertSelective(entity);
	}

	/**
	 * 更新数据
	 * 
	 * @param entity
	 * @return
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ)
	public int updateByPrimaryKey(T entity) {
		return dao.updateByPrimaryKey(entity);
	}

	/**
	 * 删除数据慎用
	 * 
	 * @param id
	 * @see public int delete(T entity)
	 * @return
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ)
	public int deleteByPrimaryKey(Integer id) {
		return dao.deleteByPrimaryKey(id);
	}
}
